/**
 * Wihidum - Efficient Broker Hierarchy for Publish Subscribe Middleware
 *
 * License      :   Apache License 2.0
 * Home Page    :   http://code.google.com/p/wihidum/
 * Mailing List :   wihidum@googlegroups.com
 */

package lk.mrt.cse.wihidum.communicator;

import lk.mrt.cse.wihidum.storage.*;
import org.w3c.dom.*;

import javax.xml.parsers.*;
import java.io.File;
import java.util.ArrayList;

public class XMLReader
{

    //private static String filePath = null;
    //private static Topology topology;

    public void readXmlDocument(String filePath)
    {
        try
        {
            //if(topology == null)
            //{
                File file = new File(filePath);
                //topology = new Topology();

                NeighbourBroker superUnit;
                NeighbourBroker peerUnit;
                NeighbourBroker subUnit;

                ArrayList<NeighbourBroker> superUnits = new ArrayList<NeighbourBroker>();
                ArrayList<NeighbourBroker> subUnits = new ArrayList<NeighbourBroker>();
                ArrayList<NeighbourBroker> peerUnits = new ArrayList<NeighbourBroker>();

                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.parse(file);
                doc.getDocumentElement().normalize();

                Element topologyElement = doc.getDocumentElement();

                NodeList topologyNameList = topologyElement.getElementsByTagName("TopologyName");
                Element topologyNameElement = (Element) topologyNameList.item(0);
                NodeList topologyName = topologyNameElement.getChildNodes();
                String strTopologyName = topologyName.item(0).getNodeValue();

                NodeList mySelfDataList = topologyElement.getElementsByTagName("MySelf");
                for (int i = 0; i < mySelfDataList.getLength(); i++) {
                    Node node = mySelfDataList.item(i);
                    if (node.getNodeType() == Node.ELEMENT_NODE && node != null) {
                        Element mySelfElement = (Element) node;

                        NodeList listID = mySelfElement.getElementsByTagName("ID");
                        Element idElement = (Element) listID.item(0);
                        NodeList id = idElement.getChildNodes();
                        Topology.setMyId(id.item(0).getNodeValue());

                        NodeList listAddress = mySelfElement.getElementsByTagName("Address");
                        Element addressElement = (Element) listAddress.item(0);
                        NodeList address = addressElement.getChildNodes();
                        Topology.setMyAddress(address.item(0).getNodeValue());

                        NodeList listRole = mySelfElement.getElementsByTagName("Role");
                        Element roleElement = (Element) listRole.item(0);
                        NodeList role = roleElement.getChildNodes();
                        Topology.setMyRole(role.item(0).getNodeValue());
                    }
                }

                NodeList listimmediateSU = topologyElement.getElementsByTagName("ImmediateSuperUnits").item(0).getChildNodes();
                for (int i = 0; i < listimmediateSU.getLength(); i++) {
                    Node node = listimmediateSU.item(i);
                    superUnit = new NeighbourBroker();

                    if (node.getNodeType() == Node.ELEMENT_NODE && node != null) {
                        Element immediateSUElement = (Element) node;

                        NodeList listID = immediateSUElement.getElementsByTagName("ID");
                        Element idElement = (Element) listID.item(0);
                        NodeList id = idElement.getChildNodes();
                        superUnit.setBroekrID(id.item(0).getNodeValue());

                        NodeList listAddress = immediateSUElement.getElementsByTagName("Address");
                        Element addressElement = (Element) listAddress.item(0);
                        NodeList address = addressElement.getChildNodes();
                        superUnit.setAddress(address.item(0).getNodeValue());

                        superUnits.add(superUnit);
                    }
                }


                NodeList listSecondarySU = topologyElement.getElementsByTagName("SecondarySuperUnits").item(0).getChildNodes();
                for (int i = 0; i < listSecondarySU.getLength(); i++) {
                    Node node = listSecondarySU.item(i);

                    if (node.getNodeType() == Node.ELEMENT_NODE && node != null) {
                        Element secondaySUElement = (Element) node;

                        NodeList listID = secondaySUElement.getElementsByTagName("ID");
                        Element idElement = (Element) listID.item(0);
                        NodeList id = idElement.getChildNodes();
                        String strID = id.item(0).getNodeValue();

                        NodeList listAddress = secondaySUElement.getElementsByTagName("Address");
                        Element addressElement = (Element) listAddress.item(0);
                        NodeList address = addressElement.getChildNodes();
                        String strAddress = address.item(0).getNodeValue();
                    }
                }


                NodeList listSubUnits = topologyElement.getElementsByTagName("SubUnits").item(0).getChildNodes();
                for (int i = 0; i < listSubUnits.getLength(); i++) {
                    Node node = listSubUnits.item(i);
                    subUnit = new NeighbourBroker();

                    if (node.getNodeType() == Node.ELEMENT_NODE && node != null) {
                        Element subUnitElement = (Element) node;

                        NodeList listID = subUnitElement.getElementsByTagName("ID");
                        Element idElement = (Element) listID.item(0);
                        NodeList id = idElement.getChildNodes();
                        subUnit.setBroekrID(id.item(0).getNodeValue());

                        NodeList listAddress = subUnitElement.getElementsByTagName("Address");
                        Element addressElement = (Element) listAddress.item(0);
                        NodeList address = addressElement.getChildNodes();
                        subUnit.setAddress(address.item(0).getNodeValue());

                        subUnits.add(subUnit);
                    }

                }


                NodeList listPeerUnits = topologyElement.getElementsByTagName("PeerUnits").item(0).getChildNodes();
                for (int i = 0; i < listPeerUnits.getLength(); i++) {
                    Node node = listPeerUnits.item(i);
                    peerUnit = new NeighbourBroker();

                    if (node.getNodeType() == Node.ELEMENT_NODE && node != null) {
                        Element peerUniElement = (Element) node;

                        NodeList listID = peerUniElement.getElementsByTagName("ID");
                        Element idElement = (Element) listID.item(0);
                        NodeList id = idElement.getChildNodes();
                        peerUnit.setBroekrID((id.item(0).getNodeValue()));

                        NodeList listAddress = peerUniElement.getElementsByTagName("Address");
                        Element addressElement = (Element) listAddress.item(0);
                        NodeList address = addressElement.getChildNodes();
                        peerUnit.setAddress(address.item(0).getNodeValue());

                        peerUnits.add(peerUnit);
                    }

                }

                Topology.setPeerUnits(peerUnits);
                Topology.setSubUnits(subUnits);
                Topology.setSuperUnits(superUnits);

                Topology.setReadOnce(true);

               // return topology;
           // }
           // else
            //{
             //   return topology;
            //}
        } catch (Exception e) {

            e.printStackTrace();
            //return null;
        }

    }

}
